package com.techwells.heartfelt.viewobject;


import java.util.HashMap;
import java.util.Map;

public class ResultViewObject {
    
    private String code;
    private Map<String, String> messages;
    private String defaultMessage;
    private Object result;
    
    public ResultViewObject() {
        this( ResultCode.SUCCESS );
    }
    
    public ResultViewObject( String code ) {
        this( code, null );
    }
    
    public ResultViewObject( String code, Object result ) {
        this( code, null, result );
    }
    
    public ResultViewObject( String code, String message ) {
        this( code, message, null );
    }
    
    public ResultViewObject( String code, String message, Object result ) {
        this.code = code;
        if( message != null && ! "".equals( message ) ) {
            this.messages = new HashMap<>();
            this.defaultMessage = message;
        }
        this.result = result;
    }
    
    public static ResultViewObject success( String message ) {
        return new ResultViewObject( ResultCode.SUCCESS, message );
    }
    
    public static ResultViewObject success( Object result ) {
        return new ResultViewObject( ResultCode.SUCCESS, result );
    }
    
    public static ResultViewObject failure( String message ) {
        return new ResultViewObject( ResultCode.FAILURE, message );
    }
    
    public static ResultViewObject failure( Object result ) {
        return new ResultViewObject( ResultCode.FAILURE, result );
    }
    
    public String getCode() {
        return code;
    }
    
    public ResultViewObject setCode(String code) {
        this.code = code;
        return this;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }
    
    public Map<String, String> getMessages() {
        return messages;
    }
    
    public String getMessage( String key ) {
        return this.messages != null ? this.messages.get( key ) : null;
    }
    
    public ResultViewObject appendMessage( String key, String message ) {
        if( key == null || "".equals( key ) ) {
            return this;
        }
        if( message == null || "".equals( message ) ) {
            return this;
        }
        if( this.messages == null ) {
            this.messages = new HashMap<>();
        }
        
        this.messages.put( key, message );
        
        return this;
    }
    
    public ResultViewObject setDefaultMessage( String message ) {        
        this.defaultMessage = message;
        return this;
    }
    
    public String getDefaultMessage() {
        return this.defaultMessage;
    }
    
}
